package fr.upstlse.glre.client.widgets.fields;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.vo.user.EtudiantVOGWT;
import fr.upstlse.glre.client.widgets.modelData.UserModelData;

public class EtudiantFields implements IUserFields {

	FormPanel formPanel;
	EtudiantVOGWT etudiant;
	TextField<String> id;
	TextField login = new TextField();
	TextField motdepasse = new TextField();
	ComboBox role;
	TextField nom;
	TextField prenom;
	TextField email;
	TextField telephoneFixe;
	TextField telephonePortable;
	TextField<String> adresseId;
	TextField adresseVoie;
	TextField adresseCodePostal;
	TextField adresseVille;
	TextField adressePays;

	boolean userFields;

	public EtudiantFields(EtudiantVOGWT pEtudiant, FormPanel pFormPanel,
			boolean pUserFields) {
		etudiant = pEtudiant;
		formPanel = pFormPanel;
		userFields = pUserFields;

		if (userFields) {
			id = new TextField<String>();
			login = new TextField();
			motdepasse = new TextField();
			role = new ComboBox();
		}

		nom = new TextField();
		prenom = new TextField();
		email = new TextField();
		telephoneFixe = new TextField();
		telephonePortable = new TextField();
		adresseId = new TextField<String>();
		adresseVoie = new TextField();
		adresseCodePostal = new TextField();
		adresseVille = new TextField();
		adressePays = new TextField();

		createFields();

		if (etudiant != null) {
			setFieldsValues();
		}
	}

	public void createFields() {
		if (userFields) {
			id.setName(RfgConstante.PROFIL_EDITING_FIELD_ID);
			id.setVisible(false);
			id.setLabelStyle("display: none");
			formPanel.add(id);
			
			login.setName(RfgConstante.PROFIL_EDITING_FIELD_LOGIN_NAME);
			login.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_LOGIN_LABEL);
			formPanel.add(login);

			motdepasse.setPassword(true);
			motdepasse
					.setName(RfgConstante.PROFIL_EDITING_FIELD_MOTDEPASSE_NAME);
			motdepasse
					.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_MOTDEPASSE_LABEL);
			formPanel.add(motdepasse);
			
			role.setName(RfgConstante.ADMIN_UTILISATEURS_CREATION_USER_ROLE_NAME);
			role.setFieldLabel(RfgConstante.ADMIN_UTILISATEURS_CREATION_USER_ROLE_LABEL);
			role.setDisplayField(RfgConstante.ROLE_MODELDATA_ROLE);
			ListStore<BaseModelData> lStore = new ListStore<BaseModelData>();
			lStore.add(UserModelData.getRoles());
			role.setStore(lStore);
			formPanel.add(role);
		}

		nom.setName(RfgConstante.PROFIL_EDITING_FIELD_NOM_NAME);
		nom.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_NOM_LABEL);
		formPanel.add(nom);

		prenom.setName(RfgConstante.PROFIL_EDITING_FIELD_PRENOM_NAME);
		prenom.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_PRENOM_LABEL);
		formPanel.add(prenom);

		email.setName(RfgConstante.PROFIL_EDITING_FIELD_EMAIL_NAME);
		email.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_EMAIL_LABEL);
		formPanel.add(email);

		telephoneFixe
				.setName(RfgConstante.PROFIL_EDITING_FIELD_TELEPHONEFIXE_NAME);
		telephoneFixe
				.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_TELEPHONEFIXE_LABEL);
		formPanel.add(telephoneFixe);

		telephonePortable
				.setName(RfgConstante.PROFIL_EDITING_FIELD_TELEPHONEPORTABLE_NAME);
		telephonePortable
				.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_TELEPHONEPORTABLE_LABEL);
		formPanel.add(telephonePortable);
		
		adresseId.setName(RfgConstante.ADRESSE_ID);
		adresseId.setVisible(false);
		adresseId.setLabelStyle("display: none");
		formPanel.add(adresseId);

		adresseVoie
				.setName(RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_VOIE_NAME);
		adresseVoie
				.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_VOIE_LABEL);
		formPanel.add(adresseVoie);

		adresseCodePostal
				.setName(RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_CODEPOSTAL_NAME);
		adresseCodePostal
				.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_CODEPOSTAL_LABEL);
		formPanel.add(adresseCodePostal);

		adresseVille
				.setName(RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_VILLE_NAME);
		adresseVille
				.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_VILLE_LABEL);
		formPanel.add(adresseVille);

		adressePays
				.setName(RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_PAYS_NAME);
		adressePays
				.setFieldLabel(RfgConstante.PROFIL_EDITING_FIELD_ADRESSE_PAYS_LABEL);
		formPanel.add(adressePays);
	}

	public void setFieldsValues() {
		if (userFields) {
			id.setValue(new Long(etudiant.getId()).toString());
			login.setValue(etudiant.getLogin());
			motdepasse.setValue(etudiant.getPassword());
			BaseModelData lRoleModelData = new BaseModelData();
			lRoleModelData.set(RfgConstante.ROLE_MODELDATA_ROLE, etudiant.getRole());
			role.setValue(lRoleModelData);
		}

		nom.setValue(etudiant.getNom());
		prenom.setValue(etudiant.getPrenom());
		email.setValue(etudiant.getEmail());
		telephoneFixe.setValue(etudiant.getTelephonefixe());
		telephonePortable.setValue(etudiant.getTelephoneportable());
		adresseId.setValue((new Long(etudiant.getAdresse().getId())).toString());
		adresseVoie.setValue(etudiant.getAdresse().getVoie());
		adresseCodePostal.setValue(etudiant.getAdresse().getCodepostal());
		adresseVille.setValue(etudiant.getAdresse().getVille());
		adressePays.setValue(etudiant.getAdresse().getPays());
	}

	public void removeFields() {
		if (userFields) {
			formPanel.remove(id);
			formPanel.remove(login);
			formPanel.remove(motdepasse);
			formPanel.remove(role);
		}

		formPanel.remove(nom);
		formPanel.remove(prenom);
		formPanel.remove(email);
		formPanel.remove(telephoneFixe);
		formPanel.remove(telephonePortable);
		formPanel.remove(adresseId);
		formPanel.remove(adresseVoie);
		formPanel.remove(adresseCodePostal);
		formPanel.remove(adresseVille);
		formPanel.remove(adressePays);
	}

	public FormPanel getFormPanel() {
		return formPanel;
	}

	public void setFormPanel(FormPanel formPanel) {
		this.formPanel = formPanel;
	}

	public EtudiantVOGWT getEtudiant() {
		return etudiant;
	}

	public void setEtudiant(EtudiantVOGWT etudiant) {
		this.etudiant = etudiant;
	}

	public TextField<String> getId() {
		return id;
	}

	public void setId(TextField<String> id) {
		this.id = id;
	}

	public TextField getLogin() {
		return login;
	}

	public void setLogin(TextField login) {
		this.login = login;
	}

	public TextField getMotdepasse() {
		return motdepasse;
	}

	public void setMotdepasse(TextField motdepasse) {
		this.motdepasse = motdepasse;
	}

	public TextField getNom() {
		return nom;
	}

	public void setNom(TextField nom) {
		this.nom = nom;
	}

	public TextField getPrenom() {
		return prenom;
	}

	public void setPrenom(TextField prenom) {
		this.prenom = prenom;
	}

	public TextField getEmail() {
		return email;
	}

	public void setEmail(TextField email) {
		this.email = email;
	}

	public TextField getTelephoneFixe() {
		return telephoneFixe;
	}

	public void setTelephoneFixe(TextField telephoneFixe) {
		this.telephoneFixe = telephoneFixe;
	}

	public TextField getTelephonePortable() {
		return telephonePortable;
	}

	public void setTelephonePortable(TextField telephonePortable) {
		this.telephonePortable = telephonePortable;
	}

	public TextField<String> getAdresseId() {
		return adresseId;
	}

	public void setAdresseId(TextField<String> adresseId) {
		this.adresseId = adresseId;
	}

	public TextField getAdresseVoie() {
		return adresseVoie;
	}

	public void setAdresseVoie(TextField adresseVoie) {
		this.adresseVoie = adresseVoie;
	}

	public TextField getAdresseCodePostal() {
		return adresseCodePostal;
	}

	public void setAdresseCodePostal(TextField adresseCodePostal) {
		this.adresseCodePostal = adresseCodePostal;
	}

	public TextField getAdresseVille() {
		return adresseVille;
	}

	public void setAdresseVille(TextField adresseVille) {
		this.adresseVille = adresseVille;
	}

	public TextField getAdressePays() {
		return adressePays;
	}

	public void setAdressePays(TextField adressePays) {
		this.adressePays = adressePays;
	}

	public ComboBox getRole() {
		return role;
	}

}
